package org.bonorsoft.modules.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.bonorsoft.common.constant.UserConstants;
import org.bonorsoft.common.util.StringUtils;
import org.bonorsoft.modules.system.entity.UserAudit;
import org.bonorsoft.modules.system.mapper.UserAuditMapper;
import org.bonorsoft.modules.system.service.IUserAuditService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserAuditServiceImpl extends ServiceImpl<UserAuditMapper, UserAudit> implements IUserAuditService {
    @Resource
    private UserAuditMapper userAuditMapper;


    /**
     * 根据条件获取用户信息
     * @param phone
     * @return
     */
    private UserAudit checkPhoneUnique(String phone) {
        return this.getOne(new QueryWrapper<UserAudit>()
                .eq(phone != null, "phone", phone).select("id", "phone"));
    }

    /**
     * 校验用户名称是否唯一
     * @param username 登录名称
     * @return 结果
     */
    @Override
    public String checkUserNameUnique(String username) {
        int count = this.count(new QueryWrapper<UserAudit>().eq("username", username));
        if (count > 0)
        {
            return UserConstants.USER_NAME_NOT_UNIQUE;
        }
        return UserConstants.USER_NAME_UNIQUE;
    }

    /**
     * 校验手机号码是否唯一
     * @param userAudit 用户信息
     * @return 结果
     */
    @Override
    public String checkPhoneUnique(UserAudit userAudit) {
        String userId = StringUtils.isNull(userAudit.getId()) ? "-1" : userAudit.getId();
        UserAudit info = checkPhoneUnique(userAudit.getPhone());
        if (StringUtils.isNotNull(info) && !info.getId().equals(userId))
        {
            return UserConstants.USER_PHONE_NOT_UNIQUE;
        }
        return UserConstants.USER_PHONE_UNIQUE;
    }
}
